package com.qire.manhua.view.comicReader.scroller;

import android.animation.ValueAnimator;
import android.view.animation.DecelerateInterpolator;

import com.qire.manhua.view.comicReader.LoopComicReaderView;

/**
 * Created by Administrator on 2018/7/28.
 */

public class ComicReadFlingAnimator extends ValueAnimator {

    private LoopComicReaderView loopComicReaderView;
    public int distanceY = 0;
    public int lastMoveDistanceY = 0;

    public ComicReadFlingAnimator(LoopComicReaderView view){
        loopComicReaderView = view;
        setFloatValues(0,1);
        setDuration(200);
        setInterpolator(new DecelerateInterpolator());
        addUpdateListener(animatorUpdateListener);
    }

    AnimatorUpdateListener animatorUpdateListener = (ValueAnimator animator)->{
        float fraction = getAnimatedFraction();
        int moveDistanceY = (int)(distanceY * fraction);
        int incrementalY = moveDistanceY - lastMoveDistanceY;
        loopComicReaderView.motionScrollY(incrementalY);
        lastMoveDistanceY = moveDistanceY;
        if(fraction == 1){
            distanceY = 0;
            lastMoveDistanceY = 0;
        }
    };

    public ComicReadFlingAnimator setDistanceY(int distanceY){
        this.distanceY = -distanceY;
        return this;
    }

}
